/****************************************************
 *      File Name:    src/utils/typeTransitions.js    
 *      Describe:     封装各种格式转换的方法
 *      Author:       Gong Keao
 ****************************************************/

//  import axios from "axios";

// 转换左侧导航的树结构
export function handleMenuTree(menuArr) {
    var arr = []
    // 先将一级菜单push到arr中
    for (const key in menuArr) {
        if (menuArr[key].menulvl === "1") {
            menuArr[key].child = []
            arr.push(menuArr[key])
        }
    }

    // 循环将二级菜单push到相应的一级菜单的child中
    for (const i in menuArr) {
        for (const j in arr) {
            if (menuArr[i].fatherid == arr[j].menuid && menuArr[i].menulvl == "2") {
                arr[j].child.push(menuArr[i])
            }
        }
    }
    return arr;
}

// 转化路由信息的树形结构
export function handleRouterTree(routerArr) {
    var arr = []
    // 先将一级路由push到arr中
    for (const key in routerArr) {
        if (routerArr[key].routerlvl === "1" && routerArr[key].routername == "index") {
            arr.push({
                routerid: routerArr[key].routerid,
                path: routerArr[key].routerpath,
                name: routerArr[key].routername,
                component: () => import(`@/views${routerArr[key].componentpath}`),
                // component: resolve => require([routerArr[key].componentpath], resolve), //将组件用require引进来
                children: []
            })
        }
    }

    // 嵌套路由
    for (const i in arr) {
        for (const j in routerArr) {
            if (routerArr[j].routerlvl == "2" && arr[i].routerid == routerArr[j].fatherid) {
                arr[i].children.push({
                    path: routerArr[j].routerpath,
                    name: routerArr[j].routername,
                    component: () => import(`@/views${routerArr[j].componentpath}`),
                    // component: resolve => require([`@/views${routerArr[j].componentpath}`], resolve),
                })
            }
        }
    }
    for (const key in arr) {
        if (arr[key].routerid) delete arr[key].routerid
    }
    return arr
}